<?php

namespace app\controller;

use app\BaseController;
use app\service\BigService;
use app\Request;
use think\facade\Config;

class Index extends BaseController
{
    protected $bigServiec;
    protected $r; //Request 对象

    public function __construct(BigService $bigServiec, Request $r)
    {
        $this->bigServiec = $bigServiec;
        $this->r = $r;
        //$this->customMethod($r);
    }

    public function index()
        //https://www.kancloud.cn/a173512/thinkphp6/1327360
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">16载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
    }


    //设置
    public function settime(Request $request)
    {
        $og = $request->post();
        $arr['controller'] = 'settime';
        $time = $og['time'];
        Config::set(['settime' => $time], 'time');
        $arr['time'] = '你设置了轮播秒数' . $time;
        $arr['config'] = Config::get('time');
        return json($arr);
    }

    public function fetchtime(Request $request)
    {
        $og = $request->post();
        $arr['controller'] = 'fetchtime';
        $arr['config'] = Config::get('time');
        return json($arr);
    }


    //texttime
    public function iotimeget(Request $request)
    {
        $file = './time.txt';
        $content = file_get_contents($file);
        $arr['time'] = $content;
        return json($arr);
    }

    public function codeget(Request $request)
    {
        $timestamp = time();
        $currentHour = date('G', $timestamp);
        $arr['cc'] = $currentHour; //17
        $arr['bigcode'] = md5($currentHour);

        return json($arr);
    }

    public function iotimeset(Request $request)
    {
        $og = $request->post();
        $time = $og['time'];
        $arr['controller'] = "iotimeset";
        $file = './time.txt';
        $handle = fopen($file, 'c');
        $content = $time;
        file_put_contents($file, $time);
        return json($arr);
    }


    public function bigshow(Request $request)
    {
        //入参是小程序post过来的
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $id = $og['id'];
        $themeid = $og['themeid'];
        $arr = $this->bigServiec->bigshow($uniacid, $id, $themeid);
        $arr['og'] = $og;
        return json($arr);
    }


    /**
     * 1.活动标题
     */
    public function activitietitle(Request $request)
    {
        //入参是小程序post过来的
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $id = $og['id'];
        $arr = $this->bigServiec->activitietitle($uniacid, $id);
        $arr['og'] = $og;
        return json($arr);
    }

    //ordersum
    //订单总量
    public function ordersum(Request $request)
    {
        //入参是小程序post过来的
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $id = $og['id'];
        $arr = $this->bigServiec->ordersum($uniacid, $id);
        $arr['og'] = $og;
        return json($arr);
    }

    //倒计时 countdown
    public function countdown(Request $request)
    {
        //入参是小程序post过来的
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $id = $og['id'];
        $arr = $this->bigServiec->countdown($uniacid, $id);
        $arr['og'] = $og;
        return json($arr);
    }

    //fullgourp
    public function fullgourp(Request $request)
    {
        //入参是小程序post过来的
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $id = $og['id'];
        $arr = $this->bigServiec->fullgourp($uniacid, $id);
        $arr['og'] = $og;
        $arr['action'] = 'fullgourp';
        return json($arr);
    }


    //6 准客，即未支付的 zhuncus
    //record
    //ut
    public function zhuncus(Request $request)
    {
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $themeid = $og['themeid'];
        $arr = $this->bigServiec->zhuncus($uniacid, $themeid);
        return json($arr);
    }

    //7 访客 ui
    public function fanssum(Request $request)
    {
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $id = $og['id'];
        $arr = $this->bigServiec->fanssum($uniacid, $id);
        return json($arr);
    }

    //ui
    public function ui(Request $request)
    {
        //入参是小程序post过来的
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $id = $og['id'];
        $arr = $this->bigServiec->ui($uniacid, $id);
        $arr['og'] = $og;
        return json($arr);
    }

    //ut
    public function ut(Request $request)
    {
        //入参是小程序post过来的
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $themeid = $og['themeid'];
        $arr = $this->bigServiec->ut($uniacid, $themeid);
        $arr['og'] = $og;
        $arr['action'] = 'ut';
        return json($arr);
    }

    //8-9 新客数量和老客数量
    //newcus 新客数量
    public function newoldcus(Request $request)
    {
        //入参是小程序post过来的
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $themeid = $og['themeid'];
        $newold = '老';
        $arr = $this->bigServiec->newoldcus($uniacid, $themeid, $newold);
        $arr['og'] = $og;
        $arr['action'] = 'newoldcus';
        return json($arr);
    }

    //10已支付
    //payon 主要是ut
    public function payon(Request $request)
    {
        //同时三个入参
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $themeid = $og['themeid'];
        $id = $og['id'];
        //先做已支付
        $arr = $this->bigServiec->payon($uniacid, $themeid);
        $arr['og'] = $og;
        $arr['action'] = 'payon';
        return json($arr);
    }

    //11 未支付
    //payoff 主要是ui
    public function payoff(Request $request)
    {
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $themeid = $og['themeid'];
        $id = $og['id'];
        //其实是访客
        $arr = $this->bigServiec->payoff($uniacid, $themeid, $id);
        $arr['og'] = $og;
        $arr['action'] = 'payoff';
        return json($arr);
    }

    //员工排行
    public function cusanking(Request $request)
    {
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $themeid = $og['themeid'];
        $id = $og['id'];
        $arr['og'] = $og;
        $arr['action'] = 'cusanking';
        $arr = $this->bigServiec->cusanking($uniacid, $themeid, $id);
        return json($arr);
    }

    //员工表
    public function worker(Request $request)
    {
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $themeid = $og['themeid'];
        $id = $og['id'];
        $arr['og'] = $og;
        $arr['action'] = 'worker';
        $arr = $this->bigServiec->worker($uniacid, $themeid, $id);
        return json($arr);
    }

    public function fans(Request $request)
    {
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $themeid = $og['themeid'];
        $id = $og['id'];
        $arr = $this->bigServiec->fans($uniacid, $themeid, $id);
        return json($arr);
    }

    public function workertop(Request $request)
    {
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $themeid = $og['themeid'];
        $id = $og['id'];
        $arr['og'] = $og;
        $arr['action'] = 'worker';
        $arr = $this->bigServiec->workertop($uniacid, $themeid, $id);
        return json($arr);
    }

    //团队排行
    public function grouptop(Request $request)
    {
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $themeid = $og['themeid'];
        $id = $og['id'];
        $arr['controller'] = 'grouptop';
        $arr = $this->bigServiec->grouptop($uniacid, $themeid, $id);
        return json($arr);
    }

    //红包排行
    public function redtop(Request $request)
    {
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $themeid = $og['themeid'];
        $arr['controller'] = 'redtop';
        $arr = $this->bigServiec->redtop($uniacid, $themeid);
        return json($arr);
    }

    //事件大屏
    public function eventslist(Request $request)
    {
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $themeid = $og['themeid'];
        $arr['controller'] = 'eventslist';
        $arr = $this->bigServiec->eventslist($uniacid, $themeid);
        return json($arr);
    }

    public function testfun(Request $request)
    {
        $og = $request->post();
        $arr['controller'] = $og;
        $array = [1, 2, 3, 4, 5, "a"];
        $slicedArray = array_slice($array, 0, 2);
        $arr['one'] = $slicedArray;
        // 1  2   3   4
        // 0  1   2   3
        $arr['two'] = array_slice($array, 2, count($array) - 2);
        //算法2
        $timestamp = time();
        $currentHour = date('G', $timestamp);
        //echo "当前时间是 $currentHour 点";
        $arr['cc'] = $currentHour; //17
        $arr['bigcode'] = md5($currentHour);
        return json($arr);
    }

    public function codeauth($bigcode)
    {
        $timestamp = time();
        $currentHour = date('G', $timestamp);
        $apicode = md5($currentHour);
        if ($bigcode == $apicode) {
            return true;
        }
        return false;
    }

    //findOrder
    public function findbyopen(Request $request)
    {
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $themeid = $og['themeid'];
        $openid = $og['openid'];
        $arr['controller'] = 'findOrder';
        $arr = $this->bigServiec->findOrder($uniacid, $themeid, $openid);
        return json($arr);
    }

    public function findbysn(Request $request)
    {
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $themeid = $og['themeid'];
        $sn = $og['sn'];
        $arr['controller'] = 'findbysn';
        $arr = $this->bigServiec->findbysn($uniacid, $themeid, $sn);
        return json($arr);
    }

    public function findbyrr(Request $request)
    {
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $themeid = $og['themeid'];
        $rr = $og['rr'];
        $arr['controller'] = 'findbyrr';
        $arr = $this->bigServiec->findbyrr($uniacid, $themeid, $rr);
        return json($arr);
    }

    public function testime(Request $request)
    {
        $og = $request->post();
        $str = $og['sql'];
        $arr['controller'] = 'testime';
        $arr = $this->bigServiec->testime($str);
        return json($arr);
    }

    public function yutest(Request $request)
    {
//        $arr['a']='a';
//        return json($arr);
        $og = $request->post();
        $text = $og['text'];
        $AppKey = "7f10bd2f882145a5b2ed1402b3b20eba";
        $AppSecret = "55643bdb52c74f2e9c269d34ba171b6f";

        $cty = new Cty();
        $url = "https://ai-global.ctapi.ctyun.cn" . "/v1/aiop/api/2z0yhhrzgv0g/tts/predict";

        $header = array();
        $header['appkey'] = 'c0b71c83e6ae902d68d9334e6410fb1c';

        if (isset($og['text'])) {
            $text = $og['text'];

        } else {
            $text = "今晚去吃火锅吗哈哈";
        }



        $body["Action"] = 'TTS';
        $body["TextData"] = $text;

        //$body = "{\"Action\": \"TTS\", \"TextData\": \"今晚去吃火锅吗\"}";

        $fileMap = array();
        $res = $cty->ctyun($AppSecret, $AppKey, $url, null, $header, $body, "POST", "application/json", null);

        return $res;
    }

    public function yuyinorderlist(Request $request){
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $themeid = $og['themeid'];
        $id = $og['id'];
        $arr = $this->bigServiec->tentopOrder($uniacid, $themeid);
        $len = count($arr);
        //便利数组
        $filteredData = array();
        $arr['$len_1'] = count($filteredData);
        $sqldata = $arr['sqldata'];
        foreach ($sqldata as $item) {
            // if (!is_null($item['username'])) {
            if (isset($item['username']) && $item['username'] !== null) {
                //$filteredData[$key] = $item;
                array_push($filteredData, $item);
            }
        }
        $arr['sqldata'] = $sqldata;
        $arr['$len_2'] = count($filteredData);
        $arr['controller'] = 'yuyinlist';
        $arr['len'] = $len;

        return json($arr);
    }

    public function yuyinone(Request $request){
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $themeid = $og['themeid'];
        $id = $og['id'];
        $recordid = $og['recordid'];
        $arr = $this->bigServiec->yuyinone($uniacid, $themeid,$recordid);
        $len = count($arr);
        //便利数组
        $filteredData = array();
        if(isset( $arr['sqldata'])){
            $sqldata = $arr['sqldata'];
            foreach ($sqldata as $item) {
                // if (!is_null($item['username'])) {
                if (isset($item['username']) && $item['username'] !== null) {
                    //$filteredData[$key] = $item;
                    array_push($filteredData, $item);
                }
            }
            unset($arr['sqldata']);
            //todo
            $this->getbasestr($filteredData);
        }else{
            $arr['备注'] = "没有数据";
        }
        $arr['$len_2'] = count($filteredData);
        $arr['controller'] = 'yuyinlist';
        $arr['len'] = $len;
        $arr['new_data'] = $filteredData;
        return json($arr);

    }


    //比如10个订单
    public function yuyinlist(Request $request)
    {
        $og = $request->post();
        $uniacid = $og['uniacid'];
        $themeid = $og['themeid'];
        $id = $og['id'];
        $arr = $this->bigServiec->tentopOrder($uniacid, $themeid);
        $len = count($arr);
        //便利数组
        $filteredData = array();
        $arr['$len_1'] = count($filteredData);
        $sqldata = $arr['sqldata'];

        foreach ($sqldata as $item) {
            // if (!is_null($item['username'])) {
            if (isset($item['username']) && $item['username'] !== null) {
                //$filteredData[$key] = $item;
                array_push($filteredData, $item);
            }
        }
        //todo
        $this->getbasestr($filteredData);
        unset($arr['sqldata']);
        $arr['$len_2'] = count($filteredData);
        $arr['controller'] = 'yuyinlist';
        $arr['len'] = $len;
        $arr['new_data'] = $filteredData;

        return json($arr);
    }

    public function t3(){
        $AppKey = "7f10bd2f882145a5b2ed1402b3b20eba";
        $AppSecret = "55643bdb52c74f2e9c269d34ba171b6f";
        $cty = new Cty();
        $url = "https://ai-global.ctapi.ctyun.cn" . "/v1/aiop/api/2z0yhhrzgv0g/tts/predict";
        $header = array();
        $header['appkey'] = 'c0b71c83e6ae902d68d9334e6410fb1c';
        $body["Action"] = 'TTS';
        $body["TextData"] = '今晚吃火锅';
        //$body = "{\"Action\": \"TTS\", \"TextData\": \"今晚去吃火锅吗\"}";
        $fileMap = array();
        $res = $cty->ctyun($AppSecret, $AppKey, $url, null, $header, $body, "POST", "application/json", null);

        $result = $res["returnObj"];

        $filePath = 'output.mp3';
        $audio_wav_dd = $this->urlSafeBase64Decode($result["Audio"]);
        file_put_contents($filePath, $audio_wav_dd);


        $audio_wav_dd = $this->urlSafeBase64Decode($result["Audio"]);
        $mp3_base64_u1 = base64_encode($audio_wav_dd);
        //$arr['audio'] = $mp3_base64_u1;
        $arr['t3'] = $filePath;
        return json($arr);
    }

    public function test()
    {

        $AppKey = "7f10bd2f882145a5b2ed1402b3b20eba";
        $AppSecret = "55643bdb52c74f2e9c269d34ba171b6f";

        $cty = new Cty();
        $url = "https://ai-global.ctapi.ctyun.cn" . "/v1/aiop/api/2z0yhhrzgv0g/tts/predict";

        $header = array();
        $header['appkey'] = 'c0b71c83e6ae902d68d9334e6410fb1c';
        $body["Action"] = 'TTS';
        $body["TextData"] = '今晚吃火锅';

        //$body = "{\"Action\": \"TTS\", \"TextData\": \"今晚去吃火锅吗\"}";

        $fileMap = array();
        $res = $cty->ctyun($AppSecret, $AppKey, $url, null, $header, $body, "POST", "application/json", null);

        $result = $res["returnObj"];
        //audio_wav_dd = base64.urlsafe_b64decode(result["Audio"])
        //print("audio base64 len:{}".format(len(audio_wav_dd)))
        $audio_wav_dd = $this->urlSafeBase64Decode($result["Audio"]);
        $mp3_base64_u1 = base64_encode($audio_wav_dd);
        $arr['audio'] = $mp3_base64_u1;
        return json($arr);

    }

    public function getbasestr(&$arr)
    {
        $AppKey = "7f10bd2f882145a5b2ed1402b3b20eba";
        $AppSecret = "55643bdb52c74f2e9c269d34ba171b6f";
        $cty = new Cty();
        $url = "https://ai-global.ctapi.ctyun.cn" . "/v1/aiop/api/2z0yhhrzgv0g/tts/predict";

        $header = array();
        $header['appkey'] = 'c0b71c83e6ae902d68d9334e6410fb1c';

        foreach ($arr as &$item) {
            $body = array();
            $body["Action"] = 'TTS';
            $body["TextData"] = $item['username'] . "  有新的订单已支付请及时处理";
            //来源员工”，您的客户“关联订单”已下单，请及时处理
            $body["TextData"] = $item['username'] . ",您的客户 ".$item['guanlian_username']."已下单，请及时处理";
            $body["VoiceType"] = 1;
            $res = $cty->ctyun($AppSecret, $AppKey, $url, null, $header, $body, "POST", "application/json", null);
            $result = $res["returnObj"];
            //audio_wav_dd = base64.urlsafe_b64decode(result["Audio"])
            //print("audio base64 len:{}".format(len(audio_wav_dd)))
            $audio_wav_dd = $this->urlSafeBase64Decode($result["Audio"]);
            $str = base64_encode($audio_wav_dd);
            $item['audio'] =$str;
            unset($body);
        }
        unset($item);
    }


    function urlSafeBase64Decode($data)
    {
        // 将 URL 安全的 Base64 字符替换回标准 Base64 字符
        $data = strtr($data, '-_', '+/');

        // 添加缺失的填充字符，确保长度为4的倍数
        $mod4 = strlen($data) % 4;
        if ($mod4 > 0) {
            $data .= substr('====', $mod4);
        }

        // 使用标准 base64_decode 进行解码
        return base64_decode($data);
    }


}
